package com.andnux.android.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import java.lang.reflect.Method;

public class Utils {
    /**
     * app是否存在
     */
    public static boolean checkApkExist(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return false;
        }
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    public static String getLastPackageName(Context context){
       return context.getPackageName().substring(context.getPackageName().lastIndexOf(".") + 1);
    }

    /**
     * 获取序列号
     * @return 序列号
     */
    public static String getDeviceSN() {
        String serial = null;
        try {
            Class<?> cla = Class.forName("android.os.SystemProperties");
            Method get = cla.getMethod("get", String.class);
            serial = (String)get.invoke(cla, "ro.serialno");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serial;
    }
}
